package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysBuyer;
import com.ruoyi.system.service.ISysBuyerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 买家Controller
 *
 * @author silencezheng
 * @date 2022-11-16
 */
@RestController
@RequestMapping("/system/buyer")
public class SysBuyerController extends BaseController
{
    @Autowired
    private ISysBuyerService sysBuyerService;

    /**
     * 查询买家列表
     */
    @PreAuthorize("@ss.hasPermi('system:buyer:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBuyer sysBuyer)
    {
        startPage();
        List<SysBuyer> list = sysBuyerService.selectSysBuyerList(sysBuyer);
        return getDataTable(list);
    }

    /**
     * 导出买家列表
     */
    @PreAuthorize("@ss.hasPermi('system:buyer:export')")
    @Log(title = "买家", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBuyer sysBuyer)
    {
        List<SysBuyer> list = sysBuyerService.selectSysBuyerList(sysBuyer);
        ExcelUtil<SysBuyer> util = new ExcelUtil<SysBuyer>(SysBuyer.class);
        util.exportExcel(response, list, "买家数据");
    }

    /**
     * 获取买家详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:buyer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysBuyerService.selectSysBuyerById(id));
    }

    /**
     * 新增买家
     */
    @PreAuthorize("@ss.hasPermi('system:buyer:add')")
    @Log(title = "买家", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBuyer sysBuyer)
    {
        sysBuyer.setCreateBy(getUsername());
        return toAjax(sysBuyerService.insertSysBuyer(sysBuyer));
    }

    /**
     * 修改买家
     */
    @PreAuthorize("@ss.hasPermi('system:buyer:edit')")
    @Log(title = "买家", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBuyer sysBuyer)
    {
        return toAjax(sysBuyerService.updateSysBuyer(sysBuyer));
    }

    /**
     * 删除买家
     */
    @PreAuthorize("@ss.hasPermi('system:buyer:remove')")
    @Log(title = "买家", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysBuyerService.deleteSysBuyerByIds(ids));
    }
}
